package org.jeecg.common.modules.register.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import org.jeecg.common.modules.register.entity.RegisterPayOrder;
import org.jeecg.common.modules.register.mapper.RegisterPayOrderMapper;
import org.jeecg.common.modules.register.service.IRegisterPayOrderService;
import org.jeecg.common.modules.order.entity.PayOrder;
import org.jeecg.common.modules.wx.entity.WxUserinfo;
import org.jeecg.common.modules.wx.service.IWxUserinfoService;
import org.jeecg.common.util.enums.OrderTypeEnum;
import org.jeecg.common.util.enums.PayOrderStatusEnum;
import org.jeecg.common.util.enums.PayOrderTypeEnum;
import org.jeecg.common.util.enums.RegisterEnum;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.Date;


/**
 * @Description: register_pay_order
 * @Author: jeecg-boot
 * @Date:   2021-01-28
 * @Version: V1.0
 */
@Service
public class RegisterPayOrderServiceImpl extends ServiceImpl<RegisterPayOrderMapper, RegisterPayOrder> implements IRegisterPayOrderService {
    @Resource
    private IWxUserinfoService wxUserinfoService;

    @Override
    public boolean checkDoing(String openid,String recordId){
        WxUserinfo wxUserinfo = wxUserinfoService.getByOpenid(openid);

        LambdaQueryWrapper<RegisterPayOrder> query = Wrappers.lambdaQuery();
        query.eq(RegisterPayOrder::getWxUserinfoId,wxUserinfo.getId());   //用户id

        query.eq(RegisterPayOrder::getRelationId,recordId);               //挂号记录id
        query.eq(RegisterPayOrder::getShopType, OrderTypeEnum.REGISTER.getCode());  //挂号业务
        query.in(RegisterPayOrder::getStatus, Lists.newArrayList(
                PayOrderStatusEnum.WAIT_PAING.getCode(),  //待支付
                PayOrderStatusEnum.SUCCESS.getCode(),     //支付成功
                PayOrderStatusEnum.REFUND_ING.getCode(),  //退款中
                PayOrderStatusEnum.REFUND.getCode()));    //退款
        int count = count(query);
        if(count == 0){
            return false;
        }else{
            return true;
        }
    }

    @Override
    public RegisterPayOrder initPayOrder(String doctorId, String relationId, String wxUserinfoId, Long amount, OrderTypeEnum orderType, String registerRecordId) {
        Date now = new Date();
        RegisterPayOrder registerPayOrder = new RegisterPayOrder();
        registerPayOrder.setOrderCode(IdUtil.getSnowflake(1,1).nextIdStr());
        registerPayOrder.setDoctorId(doctorId);
        registerPayOrder.setWxUserinfoId(wxUserinfoId);
        registerPayOrder.setRelationId(relationId);
        registerPayOrder.setAmount(Math.toIntExact(amount));
        registerPayOrder.setStartPayTime(now);
        registerPayOrder.setStatus(PayOrderStatusEnum.WAIT_PAING.getCode());
        registerPayOrder.setShopType(orderType.getCode());
        registerPayOrder.setCreateTime(now);
        registerPayOrder.setOrderType(PayOrderTypeEnum.PAY.getCode());
        registerPayOrder.setRegisterRecordId(registerRecordId);
        registerPayOrder.setInvalid("0");
        save(registerPayOrder);
        return registerPayOrder;
    }
    @Override
    public RegisterPayOrder initPayOrder(String doctorId,String relationId, String wxUserinfoId, Long amount, OrderTypeEnum orderType, String registerRecordId, String patientId) {
        Date now = new Date();
        RegisterPayOrder registerPayOrder = new RegisterPayOrder();
        registerPayOrder.setOrderCode(IdUtil.getSnowflake(1,1).nextIdStr());
        registerPayOrder.setDoctorId(doctorId);
        registerPayOrder.setWxUserinfoId(wxUserinfoId);
        registerPayOrder.setRelationId(relationId);
        registerPayOrder.setAmount(Math.toIntExact(amount));
        registerPayOrder.setStartPayTime(now);
        registerPayOrder.setStatus(PayOrderStatusEnum.WAIT_PAING.getCode());
        registerPayOrder.setShopType(orderType.getCode());
        registerPayOrder.setCreateTime(now);
        registerPayOrder.setOrderType(PayOrderTypeEnum.PAY.getCode());
        registerPayOrder.setRegisterRecordId(registerRecordId);
        registerPayOrder.setPatientId(patientId);
        registerPayOrder.setInvalid("0");
        save(registerPayOrder);
        return registerPayOrder;
    }
    @Override
    public RegisterPayOrder initPayOrder(String doctorId,String relationId, String wxUserinfoId, Long amount, OrderTypeEnum orderType, String registerRecordId, String patientId,String isShebao) {
        Date now = new Date();
        RegisterPayOrder registerPayOrder = new RegisterPayOrder();
        registerPayOrder.setOrderCode(IdUtil.getSnowflake(1,1).nextIdStr());
        registerPayOrder.setDoctorId(doctorId);
        registerPayOrder.setWxUserinfoId(wxUserinfoId);
        registerPayOrder.setRelationId(relationId);
        registerPayOrder.setAmount(Math.toIntExact(amount));
        registerPayOrder.setStartPayTime(now);
        registerPayOrder.setStatus(PayOrderStatusEnum.SUCCESS.getCode());
        registerPayOrder.setShopType(orderType.getCode());
        registerPayOrder.setCreateTime(now);
        registerPayOrder.setOrderType(PayOrderTypeEnum.PAY.getCode());
        registerPayOrder.setRegisterRecordId(registerRecordId);
        registerPayOrder.setPatientId(patientId);
        registerPayOrder.setInvalid("0");
        save(registerPayOrder);
        return registerPayOrder;
    }

    @Override
    public RegisterPayOrder getByOrderCode(String orderCode) {
        LambdaQueryWrapper<RegisterPayOrder> query = Wrappers.lambdaQuery();
        query.eq(RegisterPayOrder::getOrderCode,orderCode);
        return getOne(query);
    }
    @Override
    public RegisterPayOrder getByRelationId(RegisterPayOrder registerPayOrder) {
        LambdaQueryWrapper<RegisterPayOrder> query = Wrappers.lambdaQuery();
        query.eq(RegisterPayOrder::getRelationId, registerPayOrder.getRelationId());
        query.eq(RegisterPayOrder::getDoctorId,registerPayOrder.getDoctorId());
        query.eq(RegisterPayOrder::getStatus,registerPayOrder.getStatus());
        query.eq(RegisterPayOrder::getPatientId,registerPayOrder.getPatientId());
        query.orderByDesc(RegisterPayOrder::getCreateTime);
        query.last(" limit 1");
        return getOne(query);
    }
}
